package com.dinadi.yidi.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.CountDownTimer;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dinadi.yidi.R;
import com.dinadi.yidi.bean.ProductSubjectListBean;
import com.dinadi.yidi.ui.activity.ProductDetailsActivity;
import com.dinadi.yidi.ui.activity.ProductSubjectAllActivity;
import com.dinadi.yidi.utils.CountDownUtil;
import com.dinadi.yidi.utils.DateFormatUtils;
import com.dinadi.yidi.utils.EmptyUtils;
import com.dinadi.yidi.utils.ImageUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * author: JiaYu.He
 * Date: 2018/3/6
 * Time: 17:46
 */

public class ProductSubjectAdapter extends BaseAdapter {
    private Context context;
    private List<ProductSubjectListBean> list;
    private Map<RelativeLayout, CountDownUtil> leftTimeMap = new HashMap<RelativeLayout, CountDownUtil>();


    public ProductSubjectAdapter(Context context, List<ProductSubjectListBean> mList) {
        this.context = context;
        this.list = mList;

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_product_subject, null);
            viewHolder = new ViewHolder();
            viewHolder.img_product = (ImageView) convertView.findViewById(R.id.img_product);
            viewHolder.img_product2 = (ImageView) convertView.findViewById(R.id.img_product2);
            viewHolder.img_product3 = (ImageView) convertView.findViewById(R.id.img_product3);
            viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.product_title = (TextView) convertView.findViewById(R.id.product_title);
            viewHolder.product_title2 = (TextView) convertView.findViewById(R.id.product_title2);
            viewHolder.product_title3 = (TextView) convertView.findViewById(R.id.product_title3);
            viewHolder.product_price = (TextView) convertView.findViewById(R.id.product_price);
            viewHolder.product_price2 = (TextView) convertView.findViewById(R.id.product_price2);
            viewHolder.product_price3 = (TextView) convertView.findViewById(R.id.product_price3);
            viewHolder.special_product1 = (RelativeLayout) convertView.findViewById(R.id.special_product1);
            viewHolder.special_product2 = (RelativeLayout) convertView.findViewById(R.id.special_product2);
            viewHolder.special_product3 = (RelativeLayout) convertView.findViewById(R.id.special_product3);
            viewHolder.layout_whole = (RelativeLayout) convertView.findViewById(R.id.layout_whole);
            viewHolder.tv_hour = (TextView) convertView.findViewById(R.id.tv_hour);
            viewHolder.tv_minute = (TextView) convertView.findViewById(R.id.tv_minute);
            viewHolder.tv_sec = (TextView) convertView.findViewById(R.id.tv_sec);
            viewHolder.layout_time = (RelativeLayout) convertView.findViewById(R.id.layout_time);
            viewHolder.original_price = (TextView) convertView.findViewById(R.id.original_price);
            viewHolder.original_price2 = (TextView) convertView.findViewById(R.id.original_price2);
            viewHolder.original_price3 = (TextView) convertView.findViewById(R.id.original_price3);
            viewHolder.tv_subtitle = (TextView) convertView.findViewById(R.id.tv_subtitle);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.tv_title.setText(list.get(position).getTitle());

        if (EmptyUtils.isNotEmpty(list.get(position).getSubtitle())) {
            viewHolder.tv_subtitle.setText(list.get(position).getSubtitle());
        } else {
            viewHolder.tv_subtitle.setVisibility(View.GONE);
        }
        ImageUtils.loadImageView(context, list.get(position).getProductList().get(0).getCoverImg(), viewHolder.img_product);
        ImageUtils.loadImageView(context, list.get(position).getProductList().get(1).getCoverImg(), viewHolder.img_product2);
        ImageUtils.loadImageView(context, list.get(position).getProductList().get(2).getCoverImg(), viewHolder.img_product3);
        viewHolder.product_title.setText(list.get(position).getProductList().get(0).getTitle());
        viewHolder.product_title2.setText(list.get(position).getProductList().get(1).getTitle());
        viewHolder.product_title3.setText(list.get(position).getProductList().get(2).getTitle());

        String price = list.get(position).getProductList().get(0).getPrice();
        String originalPrice = list.get(position).getProductList().get(0).getOriginalPrice();
        viewHolder.product_price.setText("￥" + price);
        if (price != null && !price.equals(originalPrice)) {
            viewHolder.original_price.setText("￥" + originalPrice);
            viewHolder.original_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
        } else {
            viewHolder.original_price.setText("");
        }
        String price2 = list.get(position).getProductList().get(1).getPrice();
        String originalPrice2 = list.get(position).getProductList().get(1).getOriginalPrice();
        viewHolder.product_price2.setText("￥" + price2);
        if (price2 != null && !price2.equals(originalPrice2)) {
            viewHolder.original_price2.setText("￥" + originalPrice2);
            viewHolder.original_price2.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
        } else {
            viewHolder.original_price2.setText("");
        }
        String price3 = list.get(position).getProductList().get(2).getPrice();
        String originalPrice3 = list.get(position).getProductList().get(2).getOriginalPrice();
        viewHolder.product_price3.setText("￥" + price3);
        if (price3 != null && !price3.equals(originalPrice3)) {
            viewHolder.original_price3.setText("￥" + originalPrice3);
            viewHolder.original_price3.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
        } else {
            viewHolder.original_price3.setText("");
        }
        if (list.get(position).getIsLimit().equals("2")) {
            /*"是否限时 1-不限时 2-限时【int】",*/
            //获取控件对应的倒计时控件是否存在,存在就取消,解决时间重叠问题
            //leftTimeMap哪来的?接着往下看
            viewHolder.layout_time.setVisibility(View.VISIBLE);
            CountDownUtil tc = leftTimeMap.get(viewHolder.layout_time);
            if (tc != null) {
                tc.cancel();
                tc = null;
            }

            //实例化倒计时类
            CountDownUtil cdu = new CountDownUtil(list.get(position).getMillisecond(), 1000,
                    viewHolder.tv_hour, viewHolder.tv_minute, viewHolder.tv_sec, viewHolder.layout_time);
            //开启倒计时
            cdu.start();

            //[醒目]此处需要map集合将控件和倒计时类关联起来,就是这里
            leftTimeMap.put(viewHolder.layout_time, cdu);

        } else {
            viewHolder.layout_time.setVisibility(View.GONE);
        }

        viewHolder.special_product1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ProductDetailsActivity.class);
                intent.putExtra("productId", list.get(position).getProductList().get(0).getProductId());
                context.startActivity(intent);
            }
        });
        viewHolder.special_product2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ProductDetailsActivity.class);
                intent.putExtra("productId", list.get(position).getProductList().get(1).getProductId());
                context.startActivity(intent);
            }
        });
        viewHolder.special_product3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ProductDetailsActivity.class);
                intent.putExtra("productId", list.get(position).getProductList().get(2).getProductId());
                context.startActivity(intent);
            }
        });
        viewHolder.layout_whole.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ProductSubjectAllActivity.class);
                intent.putExtra("proSubId", list.get(position).getId());
                intent.putExtra("title", list.get(position).getTitle());
                context.startActivity(intent);

            }
        });
        return convertView;
    }

    class ViewHolder {
        ImageView img_product, img_product2, img_product3;
        TextView tv_title, product_title, product_title2, product_title3, product_price, product_price2, product_price3, tv_hour, tv_minute, tv_sec, original_price, original_price2, original_price3, tv_subtitle;
        RelativeLayout special_product3, special_product2, special_product1, layout_whole, layout_time;
    }

    //作为严谨的码工,当然要善始善终
    public void cancelAllTimers() {
        Set<Map.Entry<RelativeLayout, CountDownUtil>> s = leftTimeMap.entrySet();
        Iterator it = s.iterator();
        while (it.hasNext()) {
            try {
                Map.Entry pairs = (Map.Entry) it.next();
                CountDownUtil cdt = (CountDownUtil) pairs.getValue();
                cdt.cancel();
                cdt = null;
            } catch (Exception e) {
            }
        }
        it = null;
        s = null;
        leftTimeMap.clear();
    }

    private class CountDownUtil extends CountDownTimer {
        private TextView hour;//
        private TextView minute;//
        private TextView sec;//
        private RelativeLayout layout_time;


        /**
         * @param millisInFuture    倒计时时间
         * @param countDownInterval 间隔
         * @param
         */
        private CountDownUtil(long millisInFuture, long countDownInterval,
                              TextView hour, TextView minute, TextView sec, RelativeLayout layout_time) {
            super(millisInFuture, countDownInterval);
            this.hour = hour;
            this.minute = minute;
            this.sec = sec;
            this.layout_time = layout_time;

        }

        @Override
        public void onTick(long millisUntilFinished) {
//            time.setText(DateFormatUtils.getHours(millisUntilFinished));//设置时间
            hour.setText(DateFormatUtils.getOnlyHours(millisUntilFinished));//设置时间
            minute.setText(DateFormatUtils.getOnlyMinute(millisUntilFinished));//设置时间
            sec.setText(DateFormatUtils.getOnlySec(millisUntilFinished));//设置时间
            //这里接收的是毫秒值,当然,我们要将他格式化一下
        }

        @Override
        public void onFinish() {
            layout_time.setVisibility(View.GONE);
        }
    }
}
